package com.ruoyi.project.museums.controller;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.museums.domain.Question;
import com.ruoyi.project.museums.service.IQuestionService;
import com.ruoyi.project.museums.utils.WangEditorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName QuestionController
 * @Description 问题Controller
 * @Version 1.0
 */
@RestController
@RequestMapping("/museums/question")
public class QuestionController extends BaseController{
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询问题列表
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('museums:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(Question question)
    {
        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }

    /**
     * 获取问题详细信息
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('museums:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(questionService.selectQuestionById(id));
    }

    /**
     * 新增问题
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('museums:question:add')")
    @Log(title = "问题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Question question)
    {
        String currentImageSrc = question.getImage();
        if (currentImageSrc.indexOf("\"")>0){
            String[] str = currentImageSrc.split("\"");
            question.setImage(str[1]);
        }else {
            question.setImage("0");
        }


        /** 将数据存入数据库 */
        return toAjax(questionService.insertQuestion(question));
    }

    /**
     * 图片上传
     */
    @Anonymous
    @Log(title = "图片", businessType = BusinessType.UPDATE)
    @RequestMapping("/uploadImage")
    public WangEditorVO uploadImage(@RequestParam("uploadImage") MultipartFile file) throws Exception
    {
        Map data = new HashMap();
        if (!file.isEmpty())
        {
            String uploadImage = file.getOriginalFilename();
            //保存到项目的upload目录下
            file.transferTo(new File("C:\\nginx\\nginx-1.24.0\\html\\web",uploadImage));

            //因为回显的图片使用src属性，必须使用url形式，不能使用绝对路径
            String url = uploadImage;
            //放入list集合
            data.put("url", url);
            data.put("alt", uploadImage);
            return WangEditorVO.success(data); //创建Vo类并传入data属性，然后返回
        }
        return WangEditorVO.error(1,data);
    }

    /**
     * 修改问题
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('museums:question:edit')")
    @Log(title = "问题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Question question)
    {
        return toAjax(questionService.updateQuestion(question));
    }

    /**
     * 删除问题
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('museums:question:remove')")
    @Log(title = "问题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        questionService.deleteQuestionByIds(ids);
        return success();
    }
}
